﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>新增编辑</title>
    <script src="/Apm/js/CMSEdit.js" type="text/javascript"></script>
    <style>
        
    </style>
</head>
<body>
    <div class="page">
        <div id="title" class="title">
            
        </div>
        <div class="layui-form layui-form-pane edit-form">
            <div class="layui-form-item">
                <table class="layui-table">
                    <colgroup>
                        <col width="150">
                    </colgroup>
                    <thead>
                        <tr>
                            <th>种类</th>
                            <th>A</th>
                            <th>B</th>
                            <th>C</th>
                            <th>D</th>
                            <th>E</th>
                            <th>质量评核</th>
                        </tr>
                    </thead>
                    <tbody id="FlowerTypeView">
                        <!--<tr>
                            <th>玫瑰</th>
                            <th><input type="text" name="title" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
                            <th><input type="text" name="title" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
                            <th><input type="text" name="title" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
                            <th><input type="text" name="title" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
                            <th><input type="text" name="title" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
                            <th><input type="text" name="title" required placeholder="质量评核" autocomplete="off" class="layui-input"> </th>
                        </tr>-->
                    </tbody>
                </table>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea id="Remark" name="Remark" placeholder="请输入备注信息" class="layui-textarea"></textarea>
                </div>
            </div>
            
            <button id="submit" lay-submit lay-filter="submit" style="display:none;"></button>
        </div>
        
        <div class="edit-submit">
            <button id="truebutton" class="layui-btn layui-btn-lg">提交</button>
        </div>
    </div>
</body>
</html>
<script id="FlowerType" type="text/html">
    {{#  layui.each(d, function(index, item){ }}
    <tr>
        <th>{{item.Name}}</th>
        <th><input id="A{{item.Id}}" type="text" name="A{{item.Id}}" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
        <th><input id="B{{item.Id}}" type="text" name="B{{item.Id}}" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
        <th><input id="C{{item.Id}}" type="text" name="C{{item.Id}}" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
        <th><input id="D{{item.Id}}" type="text" name="D{{item.Id}}" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
        <th><input id="E{{item.Id}}" type="text" name="E{{item.Id}}" required lay-verify="number" placeholder="价格" autocomplete="off" class="layui-input"> </th>
        <th><input id="Q{{item.Id}}" type="text" name="Q{{item.Id}}" required placeholder="质量评核" autocomplete="off" class="layui-input"> </th>
    </tr>
    {{#  }); }}
</script>
<script>
    var time = MyPublic.getUrlParam('time');
    var flowerguid = MyPublic.getUrlParam('flowerguid');
    var type = MyPublic.getUrlParam('type');
    var FlowerTypeList = new Object();
    var guid = new Guid();
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer,
            form = layui.form;

        
        
        switch (type) {
            case 'add':
                $("#title").html('添加数据');
                GetFlowerType();
                break;
            case 'edit':
                $("#title").html('编辑数据');
                GetFlowerType();
                break;
        }
        $("#truebutton").click(function () {
            $("#submit").click();
        })
        //保存录入的数据
        form.on('submit(submit)', function (data) {
            //console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var count = FlowerTypeList.length;
            
            var json = new Object();
            json.FlowerGuid = flowerguid;
            json.Remark = $("#Remark").val();
            json.ListData = [];
            for (var i = 0; i < count; i++) {
                json.ListData[i] = {
                    Guid: guid.newGuid(),
                    TypeId: FlowerTypeList[i].Id,
                    FlowerGuid: flowerguid,
                    PriceA: $("#A" + FlowerTypeList[i].Id).val(),
                    PriceB: $("#B" + FlowerTypeList[i].Id).val(),
                    PriceC: $("#C" + FlowerTypeList[i].Id).val(),
                    PriceD: $("#D" + FlowerTypeList[i].Id).val(),
                    PriceE: $("#E" + FlowerTypeList[i].Id).val(),
                    Quality: $("#Q" + FlowerTypeList[i].Id).val(),
                    Time: time
                }
            }
            
            $.ajax({
                url: ApiURL + 'api/Method/FlowerInfoAdd?token=' + MyPublic.getToken(),
                type: 'post',
                contentType: 'application/json',
                data: JSON.stringify(json),
                success: function (data) {
                    data = JSON.parse(data);
                    
                    if (data.code == "10020") {
                        
                        parent.AddTab('MenuFlower', '/Public/Flower/FlowerInfo/FlowerInfoList.html', '<img src="/icon/computer_go.png" />', '花卉操作');
                        parent.DeleteTab('PublicFlowerInfoAdd');
                    }
                    else if (data.code == "10021") {
                        
                        parent.AddTab('MenuFlower', '/Public/Flower/FlowerInfo/FlowerInfoList.html', '<img src="/icon/computer_go.png" />', '花卉操作');
                        parent.DeleteTab('PublicFlowerInfoEdit');
                    }
                    else {
                        layer.msg(data.msg);
                    }
                    
                },
                error: function () {
                    location.href = "/error.html";
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    });
    /**
     * 加载花卉种类
     */
    function GetFlowerType() {
        layui.use(['layer', 'form','laytpl'], function () {
            var layer = layui.layer,
                form = layui.form,
                laytpl = layui.laytpl;

            $.ajax({
                url: ApiURL + 'api/Method/FlowerTypeList?token=' + MyPublic.getToken(),
                type: 'post',
                contentType: 'application/json',
                success: function (data) {
                    FlowerTypeList = data;
                    var getTpl = FlowerType.innerHTML
                    var view = document.getElementById('FlowerTypeView');
                    laytpl(getTpl).render(data, function (html) {
                        view.innerHTML = html;
                    });
                    form.render(); //更新全部
                    if (type == "edit") {
                        GetBindData(flowerguid);
                    }
                },
                error: function () {
                    location.href = "/error.html";

                }
            })
        })
    }
    /**
     * 编辑页面绑定数据
     * @param id
     */
    function GetBindData(id) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            $.ajax({
                url: ApiURL + 'api/Method/GetFlowerInfoList?token=' + MyPublic.getToken(),
                type: 'get',
                contentType: 'application/json',
                data: { guid: id },
                success: function (data) {
                    $("#Remark").val(MyPublic.getUrlParam('remark'));
                    if (data != null) {
                        var count = data.length;
                        for (var i = 0; i < count; i++) {
                            $("#A" + data[i].TypeId).val(data[i].PriceA);
                            $("#B" + data[i].TypeId).val(data[i].PriceB);
                            $("#C" + data[i].TypeId).val(data[i].PriceC);
                            $("#D" + data[i].TypeId).val(data[i].PriceD);
                            $("#E" + data[i].TypeId).val(data[i].PriceE);
                            $("#Q" + data[i].TypeId).val(data[i].Quality);
                        }
                        
                    }
                    
                    
                },
                error: function () {
                    location.href = "/error.html";

                }
            })
        })
        
    }

</script>